home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / cli / mx2src.arc / ATOMIC.MOD < prev    next >
Text File  |  1989-01-05  |  31KB  |  968 lines

  1.  
  2. (*              Copyright 1987,1988 fred brooks LogicTek        *)
  3. (*                                                              *)
  4. (*                                                              *)
  5. (*   First Release                      12/8/87-FGB             *)
  6. (*                                                              *)
  7. (* Modified TermProcess to also kill all the children processes *)
  8. (* of the parent. Changed FindProcess not to find zombies       *)
  9. (*                                      12/11/87-FGB            *)
  10. (*                                                              *)
  11. (* Added variable parm to StartProcess to pass info to process  *)
  12. (* in currentprocess.gemsave[15]        1/1/88-FGB              *)
  13. (* The PID of the new process will be returned in variable parm *)
  14. (*                                      2/24/88-FGB             *)
  15. (*                                                              *)
  16. (* Added DozeProcess to allow timed a sleep of processes        *)
  17. (*                                      2/21/88-FGB             *)
  18. (*                                                              *)
  19. (* Remove monitor priority. Each routine the switches processes *)
  20. (* must be protected from all interrupts by the IntEnd and      *)
  21. (* IntBegin calls. If this is not done correctly the system     *)
  22. (* system will bomb.                    4/4/88-FGB              *)
  23. (*                                                              *)
  24.  
  25.  
  26. (*$T-,$S-,$A+ *)
  27. IMPLEMENTATION MODULE          ATOMIC;
  28.  
  29. FROM  SYSTEM  IMPORT   ADR,TSIZE,
  30.                        CODE,SETREG,ADDRESS,REGISTER;
  31. FROM  NEWSYS  IMPORT   NEWPROCESS,PROCESS,TRANSFER,IOTRANSFER;
  32. FROM  XBIOS   IMPORT   SuperExec;
  33. FROM  BitStuff IMPORT   LAnd;
  34.  
  35. FROM  GEMDOS  IMPORT   GetTime,GetDate,Super;
  36.  
  37. FROM  Storage IMPORT   ALLOCATE,DEALLOCATE;
  38.  
  39. FROM  Strings IMPORT   String;
  40.  
  41. TYPE    workspace       =       ARRAY [0..254] OF CARDINAL;
  42.         savetype        =       POINTER TO ARRAY [0..22] OF CARDINAL;
  43.         sigs            =       (sleep,wakeup,terminate,trace,routine,
  44.                                  program,wait,gem,tos);
  45.         CPFlagtype      =       SET OF sigs;
  46.         CPFlagptrtype   =       POINTER TO CPFlagtype;
  47.  
  48. CONST   trapvec         =       110H; (* vector on IOTRANSFER *)
  49.         offsetvec       =       140H; (* offset IOTRANSFER vector *)
  50.         intnum          =       4;    (* interrupt number on MFP *)
  51.  
  52. VAR        s0,s1,s2,schedproc,s,
  53.            initproc,lastproc    :       SIGNAL;
  54.            kin                  :       ARRAY [0..32] OF INTEGER;
  55.            wsp0,wsp1,wsp2,wsp3  :       workspace;
  56.            wsp,bios             :       ADDRESS;
  57.            etimer [400H]        :       ADDRESS;
  58.            biospointer [4a2H]   :       ADDRESS;
  59.            ptermvec     [408H]  :       ADDRESS;
  60.            trap [trapvec]       :       ADDRESS;
  61.            memtop [436H]        :       ADDRESS;
  62.            flock [43eH]         :       CARDINAL;
  63.            hz200  [4baH]        :       LONGCARD;
  64.            temphz200,TICKS      :       LONGCARD;
  65.            sysvector [144H]     :       POINTER TO sysvariable;
  66.            accsuperstack        :       ADDRESS;
  67.            sysvar               :       sysvariable;
  68.            linea [28H]          :       ADDRESS;
  69.            gemdos [84H]         :       ADDRESS;
  70.            gsxgem [88H]         :       ADDRESS;
  71.            tbios  [0b4H]        :       ADDRESS;
  72.            xbios  [0b8H]        :       ADDRESS;
  73.            linef  [2cH]         :       ADDRESS;
  74.            level2 [68H]         :       ADDRESS;
  75.            level4 [70H]         :       ADDRESS;
  76.            shellp [4F6H]        :       ADDRESS;
  77.            oldtrap,oldtermvec   :       ADDRESS;
  78.            OldEtimer,sspval     :       ADDRESS;
  79.            p,cotick,x,t,temp    :       PROCESS;
  80.            processesid,I        :       INTEGER;
  81.            zombie               :       BOOLEAN;
  82.            children,highpri     :       INTEGER;
  83.            savefrom,saveto      :       savetype;
  84.            CPFlagptr            :       CPFlagptrtype;
  85.  
  86.         (* --------------  BEGIN -------------- *)
  87.  
  88. PROCEDURE       StartProcess(VAR P        : PROC;
  89.                              VAR n        : LONGCARD;
  90.                              VAR priority : INTEGER;
  91.                              VAR pn       : String;
  92.                              VAR parm     : ADDRESS);
  93. BEGIN
  94.         IntEnd;
  95.         SuperExec(UpdatecurrentProc);
  96.         ALLOCATE(wsp,n);
  97.         IF wsp=NIL THEN
  98.            currentprocess^.errno:=12;
  99.            IntBegin;
  100.            RETURN;
  101.         END;
  102.         DEC(sysmemsize,n);
  103.         s1:=currentprocess;
  104.         s0:=currentprocess;
  105.  
  106.         IF processesid>0 THEN    (* search only after setup of SCHED *)
  107.         s0:=schedproc;               (* set to sched process *)
  108.          LOOP                    (* find zombie process *)
  109.           s0:=s0^.next;
  110.           IF s0^.pid=1 THEN      (* zombie not found in list *)
  111.              EXIT;
  112.           END;
  113.           FindZombieProcess(zombie);
  114.           IF zombie THEN EXIT END;
  115.          END;
  116.         END; (* if processesid>1 *)
  117.  
  118.         IF zombie THEN
  119.            currentprocess:=s0;
  120.         ELSE
  121.            ALLOCATE(currentprocess,TSIZE(ProcessDescriptor));
  122.            IF currentprocess=NIL THEN
  123.               s1^.errno:=12;
  124.               IntBegin;
  125.               RETURN;
  126.            END;
  127.            DEC(sysmemsize,TSIZE(ProcessDescriptor));
  128.            INC(processesid);
  129.            currentprocess^.pid:=processesid;
  130.            currentprocess^.next:=initproc; lastproc^.next:=currentprocess;
  131.            lastproc:=currentprocess;
  132.         END;
  133.  
  134.         WITH currentprocess^ DO
  135.              name:=pn;
  136.              ppid:=request.pid;
  137.              ready:=TRUE; active:=TRUE;
  138.              tick:=0;
  139.              IF priority<1 THEN priority:=1 END;
  140.              IF priority>10 THEN priority:=10 END;
  141.              pri:=priority;
  142.              misc[0]:=pri;
  143.              IF pri>highpri THEN highpri:=pri END;
  144.              GetTime(time);
  145.              GetDate(date);
  146.              gemsave[13]:=0; (* set timer to zero *)
  147.              gemsave[14]:=0; (* set all flags to zero *)
  148.              gemsave[15]:=parm;
  149.              parm:=ADDRESS(currentprocess^.pid);
  150.              Iport:=s1^.Iport;
  151.              Oport:=s1^.Oport;
  152.         END;
  153.         IF currentprocess^.pid>1 THEN
  154.            FindProcess(request.pid,s2);
  155.            s2^.return:=currentprocess^.pid;
  156.         ELSE
  157.            currentprocess^.return:=0;
  158.         END;
  159.         SuperExec(SetSlice);
  160.         currentprocess^.slice:=temphz200;
  161.         currentprocess^.wsp:=wsp;
  162.         currentprocess^.wspsize:=n;
  163.         NEWPROCESS(P,wsp,n,currentprocess^.cor);
  164.         SuperExec(intbiosptr);
  165.         s1^.errno:=0;
  166.         INC(contextswitch);            (* update switch counter *)
  167.         TRANSFER(s1^.cor,currentprocess^.cor);
  168.         IntBegin;
  169. END     StartProcess;
  170.  
  171. PROCEDURE       TermProcess(VAR id: INTEGER);
  172. BEGIN
  173.         IF id<2 THEN RETURN END;
  174.         IntEnd;
  175.  
  176.         FindProcess(id,s2);
  177.         IF s2=NIL THEN
  178.            currentprocess^.errno:=3;
  179.            IntBegin;
  180.            RETURN;
  181.         END;
  182.         currentprocess^.errno:=0;
  183.  
  184.         s2^.active:=FALSE;      (* set flag *)
  185.         IF s2^.wsp#NIL THEN
  186.            DEALLOCATE(s2^.wsp,s2^.wspsize);
  187.            INC(sysmemsize,s2^.wspsize);
  188.         END;
  189.         s2^.wsp:=NIL;           (* set TO NIL to make zombie process *)
  190.         s0:=currentprocess; (* set to the parent process *)
  191.  
  192.         kin[0]:=id;
  193.         REPEAT (* loop thru process list and find all related processes *)
  194.         FindChildProcess(kin[0],s1);
  195.         WHILE s1#NIL DO
  196.            INC(children);
  197.            kin[children]:=s1^.pid;
  198.            s1^.active:=FALSE;      (* set flag *)
  199.            s1^.gemsave[14]:=0; (* reset all flags *)
  200.            IF s1^.wsp#NIL THEN
  201.               DEALLOCATE(s1^.wsp,s1^.wspsize);
  202.               INC(sysmemsize,s1^.wspsize);
  203.            END;
  204.            s1^.wsp:=NIL;           (* set TO NIL to make zombie process *)
  205.            currentprocess:=s1;     (* set currentprocess to terminated *)
  206.            SuperExec(IncSlice);    (* update cpu time for terminated *)
  207.            FindChildProcess(kin[0],s1